suite("notifier-list", function () {
    "use strict";

    var NotifierList = require("./notifier-list");
    var assert = require("assert");
    var Notifier = require("./notifier");
    var defineType = require("./define-type");

    test("empty", function () {
        var list = new NotifierList;
        assert(!list.items.length);
    });

    test("initialize items", function () {
        var list = new NotifierList([1, 2, 3]);
        assert(list.items.length === 3);
    });

    test("add, remove items", function () {
        var list = new NotifierList([1, 2, 3]);
        list.splice(1, 1, 4, 5, 6);
        assert(list.items.length === 5);
        assert(list.items[0] === 1);
        assert(list.items[1] === 4);
        assert(list.items[2] === 5);
        assert(list.items[3] === 6);
        assert(list.items[4] === 3);
    });

    test("trigger change", function () {
        var list = new NotifierList([1, 2, 3]);
        var called = false;
        list.on("change", function (e) {
            called = true;
            assert(e.target === list);
            assert(e.index === 1);
            assert(e.oldItems.length === 1);
            assert(e.oldItems[0] === 2);
            assert(e.newItems.length === 3);
            assert(e.newItems[0] === 4);
            assert(e.newItems[1] === 5);
            assert(e.newItems[2] === 6);
        });
        list.splice(1, 1, 4, 5, 6);
        assert(called);
    });

    test("trigger item change", function () {
        var Type = defineType(new Notifier, {
            a: 0
        });
        var a0 = new Type;
        var list = new NotifierList([1, 2, a0]);
        var called = false;
        list.on("change", function (e) {
            called = true;
            assert(e.property === "items.2.a", "property");
            assert(e.index === 2, "index");
            assert(e.target === list, "target");
        });
        a0.a = 1;
        assert(called, "called");
    });

    test("trigger new item change", function () {
        var Type = defineType(new Notifier, {
            a: 0
        });
        var a0 = new Type;
        var list = new NotifierList([1, 2, 3]);
        list.splice(2, 1, a0);
        var called = false;
        list.on("change", function (e) {
            called = true;
        });
        a0.a = 1;
        assert(called);
    });

    test("don't trigger change in removed", function () {
        var Type = defineType(new Notifier, {
            a: 0
        });
        var a0 = new Type;
        var list = new NotifierList([1, 2, 3]);
        list.splice(2, 1, a0);
        list.splice(2, 1);
        list.on("change", function (e) {
            assert(false);
        });
        a0.a = 1;
    });
});
